home *** CD-ROM | disk | FTP | other *** search
/ Turnbull China Bikeride / Turnbull China Bikeride - Disc 2.iso / BARNET / COMPILER / SATHER / !Sather / Library / Containrs / sa / member < prev    next >
Text File  |  1996-06-01  |  3KB  |  84 lines

  1. ---------------------------> Sather 1.1 source file <--------------------------
  2. -- membership.sa: Membership functions
  3. -- Author: Benedict A. Gomes <gomes@samosa.ICSI.Berkeley.EDU>
  4. -- Copyright (C) 1995, International Computer Science Institute
  5. -- $Id: member.sa,v 1.3 1996/06/01 21:36:29 gomes Exp $
  6. --
  7. -- COPYRIGHT NOTICE: This code is provided WITHOUT ANY WARRANTY
  8. -- and is subject to the terms of the SATHER LIBRARY GENERAL PUBLIC
  9. -- LICENSE contained in the file: Sather/Doc/License of the
  10. -- Sather distribution. The license is also available from ICSI,
  11. -- 1947 Center St., Suite 600, Berkeley CA 94704, USA.
  12. -------------------------------------------------------------------
  13.  
  14. class MEMBER{ETP,ATP<$CONTAINER{ETP}} is
  15.    -- Various membership functions on containers
  16.    private include COMPARE{ETP};
  17.    
  18.    find_if(a: ATP,test:ROUT{ETP}:BOOL):ETP is
  19.       -- Use inout
  20.       -- Return leftmost element of self which satisfies `test', 
  21.       -- or void if there is none. Self may be void.
  22.       loop r ::= a.elt!; if test.call(r) then return r end;  end; 
  23.       return void 
  24.    end;
  25.    
  26.    count_if(a: ATP,test:ROUT{ETP}:BOOL):INT is
  27.       -- The number of elements which satisfy `test'.
  28.       -- Self may be void.
  29.       r::=0; 
  30.       loop if test.call(a.elt!) then r:=r+1 end   end;
  31.       return r 
  32.    end;
  33.  
  34.    count(a: ATP,v:ETP):INT is
  35.       -- The number of elements that are `elt_eq' to `v'.
  36.       -- Self may be void.
  37.       r::=0; 
  38.       loop if elt_eq(a.elt!,v) then r:=r+1 end   end;
  39.       return r 
  40.    end;
  41.  
  42.    some(a: ATP,test:ROUT{ETP}:BOOL):BOOL is
  43.       -- True if some element of self satisfies `test'. 
  44.       -- Self may be void.
  45.       loop if test.call(a.elt!) then return true end  end;
  46.       return false 
  47.    end;
  48.  
  49.    every(a: ATP,test:ROUT{ETP}:BOOL):BOOL is
  50.       -- True if every element of self satisfies `test'.
  51.       -- Self may be void.
  52.       loop if ~test.call(a.elt!) then return false end  end; 
  53.       return true 
  54.    end;
  55.  
  56.    notany(a: ATP,test:ROUT{ETP}:BOOL):BOOL is
  57.       -- True if none of the elements of self satisfies `test'.
  58.       -- Self may be void.
  59.       loop if test.call(a.elt!) then return false end end; 
  60.       return true 
  61.    end;
  62.    
  63.    notevery(a: ATP,test:ROUT{ETP}:BOOL):BOOL is
  64.       -- True if not every element of self satisfies `test'.
  65.       -- Self may be void.
  66.       loop if ~test.call(a.elt!) then return true end  end;
  67.       return false 
  68.    end;
  69.  
  70.    filter!(once a: ATP,once f:ROUT{ETP}:BOOL): ETP  pre ~void(a) is
  71.       loop
  72.      e ::= a.elt!;
  73.      if f.call(e) then yield e end
  74.       end
  75.    end;
  76.  
  77.    filter_not!(once a: ATP,once f:ROUT{ETP}:BOOL): ETP  pre ~void(a) is
  78.       loop e ::= a.elt!; if ~f.call(e) then yield e end  end
  79.    end;
  80.  
  81. end; -- class MEMBER{ETP,ATP<$CONTAINER{ETP}}
  82. -------------------------------------------------------------------
  83.  
  84.